package com.czy.owner.ui.collection;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.view.View;

import com.czy.owner.R;
import com.czy.owner.adapter.CommonTabLayoutAdapter;
import com.czy.owner.db.UserHelper;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by wangyang on 2017/5/16 0016.
 * 收藏
 */

public class CollectionActivity extends BaseActivity {
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;
    private List<String> titles;
    private CommonTabLayoutAdapter mFragmentAdapter;
    private List<BaseFragment> mFragments = new ArrayList<>();

    @Override
    protected int attachLayoutRes() {
        return R.layout.collection_activity_tablayout;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.my_collect);
        initViewPager();
    }

    @Override
    protected void updateViews() {

    }


    private void initViewPager() {
        if (UserHelper.getInstance().isCloseMall()) {
            mTabLayout.setVisibility(View.GONE);
        } else {
            mTabLayout.setVisibility(View.VISIBLE);
        }
        addTabTitle();
        addTabFragment();
        mFragmentAdapter = new CommonTabLayoutAdapter(getSupportFragmentManager(), mFragments, titles);
//        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapter);
        viewPager.setOffscreenPageLimit(titles.size());
        //将TabLayout和ViewPager关联起来。
        mTabLayout.setupWithViewPager(viewPager);
    }


    private void addTabTitle() {
        titles = new ArrayList<>();
        titles.add("商品");
        if (!UserHelper.getInstance().isCloseMall()) {
            titles.add("资讯");
            titles.add("攻略");
        }
        for (int i = 0; i < titles.size(); i++) {
            mTabLayout.addTab(mTabLayout.newTab().setText(titles.get(i)));
        }

    }


    private void addTabFragment() {
        GoodCollectionFragment goodCollectionFragment = new GoodCollectionFragment();
        InformationCollectionFragment informationCollectionFragment = new InformationCollectionFragment();
        StrategyCollectionFragment strategyCollectionFragment = new StrategyCollectionFragment();
        mFragments.add(goodCollectionFragment);
        if (!UserHelper.getInstance().isCloseMall()) {
            mFragments.add(informationCollectionFragment);
            mFragments.add(strategyCollectionFragment);
        }
    }
}
